using System;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using System.Runtime;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;

namespace SiRtEt
{
	/// <summary>
	/// Summary description for HallOfFame.
	/// </summary>
	public class HallOfFame : CollectionBase 
	{
		#region Variables
		private BinaryFormatter binaryFormatter = new BinaryFormatter();
		string HOFFile;
		object HOF = new object();
		#endregion

		#region Constructor
		/// <summary>
		/// Constructor : initializes the components
		/// </summary>
		public HallOfFame()
		{
			binaryFormatter = new BinaryFormatter();
			string dir = Application.UserAppDataPath;			// folder in which app is running
			HOFFile  = dir + @"\" + "HallOfFame.tan";	// filename for HOF

			if(!ReadHOF() || HOF == null)
				CreateDummyHOF();
		}
		#endregion

		#region Create a Dummy Hall of Fame 
		private void CreateDummyHOF()
		{
			Player dummyPlayer;
			for(int i=0;i<5;i++)
			{
				dummyPlayer = new Player();
				Insert(dummyPlayer);
			}
		}
		#endregion

		#region Read Hall of Fame
		/// <summary>
		/// Read the HOF from the file
		/// </summary>
		/// <returns></returns>
		private bool ReadHOF()
		{
			if(!File.Exists(HOFFile))		// check if file exists
				return false;

			FileStream fileStream = File.Open(HOFFile,FileMode.Open,FileAccess.Read); // Open to read

			try		// read the file after deserializing it, close the file, return
			{
				HOF = binaryFormatter.Deserialize(fileStream);
				fileStream.Close();
				return true;
			}
			catch (Exception e) // if error in above methods, close the file and return false
			{
				fileStream.Close();
				MessageBox.Show("Hall of Fame Read Error "+ e.Message);
				return false;
			}
		}
		#endregion

		#region Write to Hall of Fame
		/// <summary>
		/// Write the HOF to the file
		/// </summary>
		/// <returns></returns>
		private bool WriteHOF()
		{
			FileStream fileStream = File.Open(HOFFile,FileMode.OpenOrCreate,FileAccess.Write);
			try
			{
				binaryFormatter.Serialize(fileStream, HOF);
				fileStream.Close();
				return true;
			}
			catch (Exception e)
			{
				fileStream.Close();
				MessageBox.Show("Could not write Hall of Fame " + e.Message);
				return false;
			}
		}
		#endregion

		#region Show Hall of Fame
		/// <summary>
		/// Function to show the Hall of Fame form
		/// </summary>
		public void ShowHOF(ListView listView)
		{
			// reading the HOF from file
			//if(!ReadHOF())
			//	CreateDummyHOF(); // if that doesnt exist, create a new one
			
			for(int i =0;i<Count;i++)
			{
				Player toAdd = InnerList[i] as Player;

				ListViewItem toAddToList = new ListViewItem(toAdd.PlayerName);
				toAddToList.SubItems.Add(toAdd.PlayerScore.ToString());
				toAddToList.SubItems.Add(toAdd.PlayerLines.ToString());

				listView.Items.Add(toAddToList);
			}            
		}
		#endregion

		#region Check the Hall of Fame and update it if required
		/// <summary>
		/// To update the hall of fame
		/// </summary>
		/// <returns></returns>
		public void Check(Player player)
		{
			InnerList.Sort();		// Sort the values already in HOF

			for(int i=0;i<InnerList.Count;i++)
			{
				Player inHOF = InnerList[i] as Player;
				if(inHOF.CompareTo(player)==-1)
				{
					InnerList.RemoveAt(i);

					// Code to get players name
	
					player.PlayerName = "Tan";

					InnerList.Insert(i,player);
					InnerList.Sort();
					WriteHOF();
					break;
				}
			}
		}
		#endregion

		private void Insert(Player value)
		{
			InnerList.Add(value as object);
		}
		private void Delete(Player value)
		{
			InnerList.Remove(value as object);
		}
		private bool Contains(Player value)
		{
			return InnerList.Contains(value as object);
		}
	}
}
